/*
Copyright (c) 2025 HeZongLun
NahidaProject is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

#pragma once

#ifdef _WIN32
#define ExportSymbol  __declspec(dllexport)
#else
#define ExportSymbol
#endif

#include<string>
#include<fstream>

#define MYFILE(x) strrchr(x, '\\') ? strrchr(x, '\\') + 1 : x
#define Debug(format,...) NahidaProject::Logger::GetInstance()->Log(NahidaProject::Logger::DEBUG, MYFILE(__FILE__), __LINE__,format,##__VA_ARGS__);
#define Information(format,...) NahidaProject::Logger::GetInstance()->Log(NahidaProject::Logger::INFO, MYFILE(__FILE__), __LINE__,format,##__VA_ARGS__);
#define Warning(format,...) NahidaProject::Logger::GetInstance()->Log(NahidaProject::Logger::WARN, MYFILE(__FILE__), __LINE__,format,##__VA_ARGS__);
#define Error(format,...) NahidaProject::Logger::GetInstance()->Log(NahidaProject::Logger::ERRO, MYFILE(__FILE__), __LINE__,format,##__VA_ARGS__);
#define Fatal(format,...) NahidaProject::Logger::GetInstance()->Log(NahidaProject::Logger::FATAL, MYFILE(__FILE__), __LINE__,format,##__VA_ARGS__); exit(-1);

namespace NahidaProject {
	class ExportSymbol Logger {

	public:
		enum Level {DEBUG = 0, INFO, WARN, ERRO, FATAL, LEVEL_COUNT };
		void Open(const std::string& fileName);
		void Close();
		static Logger* GetInstance();
		void Log(Level level, const char* fileName, int line, const char* format, ...);
		void SetLevel(Level level)
		{
			levels = level;
		}

		void SetMax(int bytes){
			max = bytes;
		}
		void Rotate();

	private:
		Logger();
		~Logger();

	private:
		std::string fileName;
		std::fstream fileOut;
		static const char* level[LEVEL_COUNT];
		static Logger* instance;
		Level levels;
		int max;
		int length;
	};
}
